﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BuildPlannerLib.Enums;

namespace BuildPlannerLib
{
    // Class for setting and defining map properties in the simulation
    public class Map
    {
        // List of bases on the map.
        public List<Base> Bases;

        // Bitwise flags for the starting 6 mineral patches,starting with the center
        // patch and moving outward from there
        public MineralFlags[] StartingMineralFlags;

        // Integer readonly properties for the total number of mineral and gas workers
        public int MineralWorkers
        {
            get
            {
                int mineralWorkers = 0;
                Bases.ForEach(delegate(Base b) { mineralWorkers += b.MineralWorkers; });
                return mineralWorkers;
            }
        }
        public int GasWorkers
        {
            get
            {
                int gasWorkers = 0;
                Bases.ForEach(delegate(Base b) { gasWorkers += b.GasWorkers; });
                return gasWorkers;
            }
        }

        // Constructor for building the map from the enum type MapSelect
        // Unfinished
        public Map(MapSelect mapSelect)
        {
            switch (mapSelect)
            {
                default:
                    StartingMineralFlags = new MineralFlags[] {MineralFlags.FarPatch, MineralFlags.ClosePatch, 
                        MineralFlags.FarPatch, MineralFlags.FarPatch, MineralFlags.FarPatch, MineralFlags.ClosePatch, 
                        MineralFlags.FarPatch, MineralFlags.FarPatch };
                    int[] mineralArray = new int[] { 6, 0, 2, 0 };
                    int[] gasArray = new int[] { 2, 0, 0 };

                    Bases = new List<Base>();

                    Bases.Add(new Base(mineralArray, gasArray));
                    mineralArray[0] = 4;
                    mineralArray[2] = 4;
                    Bases.Add(new Base(mineralArray, gasArray));
                    Bases.Add(new Base(mineralArray, gasArray));
                    Bases.Add(new Base(mineralArray, gasArray));
                    break;
            }
        }

        // Constructor that takes the flags for all mineral patches at the main base
        // and converts it into StartingPatches[]
        public Map(MineralFlags[] mineralFlags, int[] gasLayout)
        {
            StartingMineralFlags = mineralFlags;
            int[] minerallayout = new int[] { 0, 0, 0, 0 };
            foreach (MineralFlags i in mineralFlags)
            {
                minerallayout[(int)i]++;
            }

            Bases = new List<Base>();

            AddBase(minerallayout, gasLayout);

            Bases.First().MineralWorkers = 6;
            Bases.First().Status = BaseStatus.ExpandFinished;
        }
        // Calls the Base constructor for a new base to be added to the map.
        public void AddBase(int[] minerallayout, int[] gaslayout)
        {
            Bases.Add(new Base(minerallayout, gaslayout));
        }

        // Method for resetting the map
        public void Reset()
        {
            // Resets each base added to the map
            Bases.ForEach(delegate(Base b) { b.Reset(); } );

            // Sets the first base to have 6 workers and a finished main building
            Bases.First().MineralWorkers = 6;
            Bases.First().Status = BaseStatus.ExpandFinished;
        }
    }
}